/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2020-06-28 11:10:30
 * @LastEditors: kl2018
 * @LastEditTime: 2020-08-11 09:48:13
 */
import React from 'react';
import { isEqual, isEmpty } from 'lodash';
import { WeaErrorPage, WeaLocaleProvider, WeaTools } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
import OpToolbar from '../common/OpToolbar';
import TitleTabPane from '../common/TitleTabPane';

let Base64 = ecCom.WeaTools.Base64;
import { handleWidth } from '../../util/common';
// tab的标题组件
class Title extends React.Component {
  constructor(props) {
    super(props);
    this.setMoreHref = this.setMoreHref.bind(this);
  }
  componentDidMount() {
    const { config, tabids } = this.props;
    const { eid, header } = config.item;
    this.setMoreHref();
    handleWidth(eid, config.params.hpid, tabids.length, header.canHeadbar, header.displayPosition);
  }
  componentDidUpdate() {
    const { config, tabids } = this.props;
    const { eid, header } = config.item;
    this.setMoreHref();
    handleWidth(eid, config.params.hpid, tabids.length, header.canHeadbar, header.displayPosition);
  }
  shouldComponentUpdate(nextProps) {
    return !isEqual(this.props, nextProps);
  }
  setMoreHref() {
    const { config, more, currentTab } = this.props;
    const { eid } = config.item;
    if (more && JSON.parse(more).viewType != '6') {
      window.elements_more_params[eid] = { isRouteUrl: true, paramsStr: `&jsonstr=${Base64.encode(more.toString())}` };
    } else {
      window.elements_more_params[eid] = { isRouteUrl: false, paramsStr: `&tabid=${currentTab}` };
    }
  }
  render() {
    const { config, tabids = [], handleRefresh, handleDelete, fontSizeMp, customClickMore, needMore } = this.props;
    const { eid, ebaseid } = config.params;
    const { header } = config.item;
    let { fontStyle = {} } = config.item;
    if (!isEmpty(fontSizeMp)) {
      fontStyle = fontSizeMp;
    }
    let className = tabids.length <= 1 ? 'nodisplay' : '';
    if (header.displayPosition && header.displayPosition == 'bottom') {
      return (<div id={`titleContainer_${eid}`} className={`titlecontainer ${className}`}>
        <TitleTabPane ecId={`${this && this.props && this.props.ecId || ''}_TitleTabPane@en1cpl`} datas={this.props} fontStyle={fontStyle} ebaseid={ebaseid} />
        {!config.item.header.canHeadbar ?
          <OpToolbar ecId={`${this && this.props && this.props.ecId || ''}_OpToolbar@61ufj3`}
            config={config}
            clsname={className}
            handleRefresh={handleRefresh}
            handleDelete={handleDelete}
            needMore={needMore}
            customClickMore={customClickMore}
          /> : ''
        }
      </div>);
    }
    return <div />;
  }
}
class MyErrorHandler extends React.Component {
  render() {
    const hasErrorMsg = this.props.error && this.props.error !== '';
    return (<WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@3i77mh`} msg={hasErrorMsg ? this.props.error : getLabel(385218, '对不起，该页面异常，请联系管理员！')} />
    );
  }
}
Title = WeaTools.tryCatch(React, MyErrorHandler, { error: '' })(Title);
export default Title;
